package com.spsy.order.mq.listener;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@RabbitListener(queues = "TestDirectQueue2206a")//监听的队列名称 TestDirectQueue
public class DirectReceiver {

    /*@RabbitHandler
    public void process(Map testMessage) {
        System.out.println("======>order ---> DirectReceiver2206a 消费者收到消息  : " + testMessage.toString());
    }*/


   /*@RabbitHandler
    public void process(String testMessage , Channel channel) throws Exception {
        System.out.println("======>1111order ---> DirectReceiver2206a 消费者收到消息  : " + testMessage.toString());
        try {
            // 处理消息
            //String body = new String(testMessage.getBody());
            System.out.println("Received: " + testMessage);

            // 手动确认
           channel.basicAck(testMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 处理失败，拒绝消息并重新入队
            //channel.basicNack(testMessage.getMessageProperties().getDeliveryTag(), false, true);
        }

    }*/




    @RabbitHandler
    public void process(Map testMessage, Message message, Channel channel) {
        System.out.println("======>DirectReceiver2206a 消费者收到消息  : " + testMessage.toString());

        try {
            //确认回调
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);


            System.out.println("从新入队");

            //处理失败、拒绝消息并重新入队
            //channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}